using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;


namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class FileLoopInstruction:InstructionBase
    {
        private string _FolderPath;

        public string FolderPath
        {
            get { return _FolderPath; }
            set { _FolderPath = value; }
        }

        private string _FileNamePattern;

        public string FileNamePattern
        {
            get { return _FileNamePattern; }
            set { _FileNamePattern = value; }
        }

        private SearchOption _SearchOp;

        public SearchOption SearchOption
        {
            get { return _SearchOp; }
            set { _SearchOp = value; }
        }
	
	
        public FileLoopInstruction()
            :base(Guid.Empty, Guid.Empty)
        {
            this._FolderPath = string.Empty;
            this._FileNamePattern = string.Empty;
            this._SearchOp = System.IO.SearchOption.TopDirectoryOnly;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="folderPath"></param>
        /// <param name="fileNamePattern"></param>
        /// <param name="searchOp"></param>
        public FileLoopInstruction(Guid componentID, Guid jobTicketID, 
            string folderPath, string fileNamePattern, SearchOption searchOp)
            : base(componentID, jobTicketID)
        {
            this._FolderPath = folderPath;
            this._FileNamePattern = fileNamePattern;
            this._SearchOp = searchOp;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode instructionNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "FolderPath", this.FolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "FileNamePattern", this.FileNamePattern);
            XmlDataUtil.UpdateAttribute(ref xDoc,instructionNode,"SearchOption",((int)this.SearchOption).ToString());

            return instructionNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileLoopInstruction instruction = new FileLoopInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.FolderPath = xNode.Attributes.GetNamedItem("FolderPath").Value;
                instruction.FileNamePattern = xNode.Attributes.GetNamedItem("FileNamePattern").Value;
                instruction.SearchOption = (SearchOption) int.Parse(xNode.Attributes.GetNamedItem("SearchOption").Value);

                return instruction;
            }
            else
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
        }
    }
}
